package hu.szyrt.bug.utils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

public class Utils {

	public static int getDPSize(int size, Resources res){
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, res.getDisplayMetrics());
	}
	
	public static int getDPSizeByScreenSize(int small, int normal, int large, int xlarge, Resources res){
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, getSizeByScreenSize(small, normal, large, xlarge, res), res.getDisplayMetrics());
	}
	
	public static int getSizeByScreenSize(int small, int normal, int large, int xlarge, Resources res){
		int screenSize = res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
		return new int[]{normal, small, normal, large, xlarge}[screenSize];
	}

	public static void hideKeyboard(View v){
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);        
    }
	
	public static boolean isInternetAvailable(Context context){
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		return cm.getActiveNetworkInfo() != null && 
			   cm.getActiveNetworkInfo().isAvailable() && 
			   cm.getActiveNetworkInfo().isConnected();
	}
	
	public static boolean isSDAvailable(){
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
	
	public static String join(Collection<?> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<?> iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext())
                buffer.append(delimiter);            
        }
        return buffer.toString();
    }
		
	public static String join(String[] collection, String delimiter){
		return Utils.join(Arrays.asList(collection), delimiter);
	}
	
	public static String inputStreamToString(InputStream stream){
		BufferedReader br = new BufferedReader(new InputStreamReader(stream));
		String line = null;
		StringBuilder sb = new StringBuilder();
		try {
			while((line = br.readLine()) != null)
				sb.append(line);
			br.close();			
		} catch (IOException e) {}		
		return sb.toString();
	}
	
	public static String supportedLanguage(String desiredLanguage){
		if(Arrays.asList(Config.SUPPORT_LANGUAGES).contains(desiredLanguage))
			return desiredLanguage;
		else 
			return Config.SUPPORT_LANGUAGES[0];
	}
	
	public static void copyFile(InputStream in, OutputStream out) throws IOException {
	    byte[] buffer = new byte[2048]; int read;
	    while((read = in.read(buffer)) != -1)
	      out.write(buffer, 0, read);
	}
	
	
	
	public static void stripUnderlines(TextView textView) {
        Spannable s = (Spannable)textView.getText();
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span: spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);
        }
        textView.setText(s);
    }

	
	// Other --------
	private static class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
            super(url);
        }
        @Override public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }

}
